package com.hbnu.controller;

import com.hbnu.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
@RequestMapping("vip")
public class HelloServlet  {
    @RequestMapping("/hello") // /vip/hello
    public String hello(){
        return "index.jsp";
    }
    // 参数
    @RequestMapping("/hello2") // /vip/hello2
    @ResponseBody
    public void hello(String param1, String param2){
        System.out.println(param1);
        System.out.println(param2);
    }
    // 对象参数
    @RequestMapping("/hello3")
    @ResponseBody
    public void hello3(User user){
        System.out.println(user);
    }
    // 路径参数
    @RequestMapping("/hello4/{username:[a-z]+}/{address}")
    @ResponseBody
    public void hello4(@PathVariable("username") String name, @PathVariable String address){
        System.out.println(name);
        System.out.println(address);
    }

    @RequestMapping("/hello5")
    @ResponseBody
    public void hello5(Model model){
        System.out.println(model);
    }

    @RequestMapping("/redirect")
    public String redirect(Model model){
        model.addAttribute("username","zhangsan");
        return "redirect:/redirect.jsp";
    }
    @RequestMapping("/forward")
    public String forward(Model model){
         model.addAttribute("username","zhangsan");
        return "forward:/forward.jsp";
    }
    @RequestMapping("/dome1")
    public void dome1(Model model){
        User user = new User();
        user.setUsername("zhangsan");
        user.setAddress("beijing");
        // model.addAttribute("user",user);
        model.addAttribute("user",user);
        System.out.println(user);
    }
    @RequestMapping("/dome2")
    @ResponseBody
    public void dome2(Model model) {
        User user = (User) model.getAttribute("user");
        System.out.println(user);
    }

    @RequestMapping("/dome3")
    @ResponseBody
    public void dome3(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        session.setAttribute("key","value");
    }
    // 日期参数
    @RequestMapping("/dome4")
    @ResponseBody
    public void dome4(Date date) {
        System.out.println(date);
    }
    // 解决乱码
    @RequestMapping(value = "/dome5", produces = "text/html;charset=utf-8")
    @ResponseBody
    public String dome5() {
        return "这是一个中文信息";
    }
    // 返回对象
    @RequestMapping("/dome6")
    @ResponseBody
    public User dome6() {
        User user = new User();
        user.setUsername("zhangsan");
        user.setAddress("beijing");
        return user;
    }
}
